package com.newear.datacenter;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.util.ArrayMap;
import android.util.SparseArray;

import com.newear.constant.DataType;
import com.newear.constant.RequireParams;
import com.newear.message.MessageDispatcher;
import com.newear.message.MessageType;
import com.newear.util.MyLogger;
import com.newear.util.Validator;

/**
 * Created by josan_tang on 2016/07/19.
 * 数据请求操作类
 */
public class DataReuireOperate {
    private static final String TAG = "DataReuireOperate";
    private ArrayMap<String, Object> params;    //请求参数
    private SparseArray<Object> results;   //请求结果  result[0]-key;result[1]-result
    private Handler appHandler; //Application中的Handler对象
    private int dataType;   //数据类型
    private Context appContext;

    public DataReuireOperate(Context context, Handler appHandler, ArrayMap<String, Object> params) {
        results = new SparseArray<Object>();
        this.appContext = context;
        this.appHandler = appHandler;
        this.params = params;
    }

    /**
     * 真正的数据请求
     *
     * @param asyncTask 异步请求的任务
     */
    public void requireOprate(AsyncTask<Object, Object, Object> asyncTask) {
        MyLogger.i(TAG, "requireOprate!");
        if (params == null) {
            return;
        }

        String key = (String) params.get(RequireParams.MAP_KEY);  //
        dataType = (Integer) params.get(RequireParams.DATA_TYPE);
        if (Validator.isEffective(key)) {
            results.put(0, key);
            DataProvider dataProvider = DataProvider.getInstance();
            switch (dataType) {
                case DataType.UPLOAD_FACE_PHOTO:
                    SparseArray<Object> facePhotoResult = dataProvider.uploadFacePhoto(this.appContext);
                    results.put(1, facePhotoResult);
                    params.clear();
                    break;
                case DataType.UPLOAD_TONGUE_PHOTO:
                    SparseArray<Object> tonguePhotoResult = dataProvider.uploadTonguePhoto(this.appContext);
                    results.put(1, tonguePhotoResult);
                    params.clear();
                    break;
                case DataType.USER_LOGIN:    //用户登录  results(0)-key;result(1)-result[2]
                    SparseArray<Object> result = dataProvider.userLogin(this.appContext,
                            (String) params.get(RequireParams.USER_NAME),
                            (String) params.get(RequireParams.PASSWORD));
                    results.put(1, result);
                    params.clear();
                    break;
                case DataType.GET_WEATHER:
                    SparseArray<Object> weatherResult = dataProvider.getWeather(this.appContext,
                            (double)params.get(RequireParams.LAT),
                            (double)params.get(RequireParams.LON));
                    results.put(1, weatherResult);
                    params.clear();
                    break;
                case DataType.GET_REPORT:
                    SparseArray<Object> reportResult = dataProvider.getReport(this.appContext);
                    results.put(1, reportResult);
                    params.clear();
                    break;
                case DataType.HISTORY_REPORT:
                    SparseArray<Object> distoryReportResult = dataProvider.getHistoryReport(this.appContext,
                            (String)params.get(RequireParams.DATE),
                            (int)params.get(RequireParams.PAGE));
                    results.put(1, distoryReportResult);
                    params.clear();
                    break;
                case DataType.GET_REPORT_BY_ID:
                    SparseArray<Object> reportResultById = dataProvider.getReportById(this.appContext,
                            (int)params.get(RequireParams.ID));
                    results.put(1, reportResultById);
                    params.clear();
                    break;
                case DataType.CHECK_VIP:
                    SparseArray<Object> checkVipResult = dataProvider.checkVip(this.appContext,
                            (String)params.get(RequireParams.ID));
                    results.put(1, checkVipResult);
                    params.clear();
                    break;
                default:
                    break;
            }
        } else {
            MyLogger.e(TAG, "mapkey is null!");
        }
        if (asyncTask.isCancelled() == false) {  //如果当前任务没有被取消，则发消息给Application中的handler,通知数据请求完成
            MessageDispatcher.sendMessage(appHandler, MessageType.REQUIRE_DATA_FINISHED, dataType, results);
        }
        appHandler = null;
    }
}
